<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UserMedicineOrderTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function saveOrder($order)
    {
		$data = array(
			'user_id' 	  	       		=> $order['user_id'], 				
			'prescription_details' 		=> $order['prescription_details'],  		
			'prescription_upload_path' 	=> $order['prescription_upload_path'],		
			'auto_renewal'  	        => $order['auto_renewal'],  	
			'each_days' 	           	=> $order['each_days'],
			'date_added'	  	       	=> date('Y-m-d H:i:s'), 	
			'date_updated' 		       	=> date('Y-m-d H:i:s'),   			
			'status' 		       		=> 0, 			
		);		
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }	
	public function checkOrder($order_id)
    {	
		$select = $this->tableGateway->getSql()->select();			
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();		
		return $row;		
	}
	public function orderStatus($order_id){ 
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function getOrderDetails($order_id){ 
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_medicine_order_id="'.$order_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
	public function totalOrders($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');
		$select->where('user_medicine_order.user_id="'.$user_id.'"');
		$alllist = $this->tableGateway->selectWith($select);	
		return $alllist;
	}
	public function updateOrderRenewal($orderId,$auto_renewal)
    {	
		$data = array(
			'auto_renewal' 	=> $auto_renewal, 
			'date_updated'	=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function cancelledOrder($orderId)
    {	
		$data = array(
			'status' 		=> 3, 
			'date_updated'	=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;			
	}
	public function getAllUserOrders(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=user_medicine_order.user_medicine_order_id'),array('price','discount','shop_id','payment_status','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');
		$result = $this->tableGateway->selectWith($select);	
		return $result;
	}
	public function getAllRenewedOrders()
	{
		$select = $this->tableGateway->getSql()->select();
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('location','pincode','city','state','country','address'),'left');
		$select->where('user_medicine_order.auto_renewal="1"');
		$select->where('user_medicine_order.status="1"');
		$select->where('user_medicine_order.each_days=DATEDIFF(now(),user_medicine_order.date_added)+1');
		$result = $this->tableGateway->selectWith($select);	
		//echo "<pre>";print_r($result);exit;
		return $result;
	}
	public function changeAutoRenewal($orderId,$order)
	{
		$data = array(
			'auto_renewal' 		=> $order['auto_renewal'], 
			'date_updated'	=> date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $orderId));
		return 	$updaed_status;
	}
}